package com.eazy.lkv2.web.controller.system;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.service.system.SystemPermissionService;
import com.eazy.lkv2.web.service.system.SystemLogService;
import com.google.common.collect.Maps;

/**
 * 首页展示
 * 
 * @date 2016.06.16
 */
@Controller
@RequestMapping("system")
public class SystemIndexController extends BaseController {

	@Autowired
	private SystemLogService systemLogService;
	@Autowired
	private SystemPermissionService permissionService;

	/**
	 * 跳转到首页
	 */
	@RequiresAuthentication
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login() {
		List<Map<String, Object>> result = permissionService.permissionList(getCurrentUser().getId().toString());
		addObject("data", result);
		return getSuccess("index/index");
	}

	/**
	 * 跳转到欢迎页
	 */
	@RequiresAuthentication
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public ModelAndView welcome() {
		Map<String, Object> view = Maps.newHashMap();

		List<Map<String, Object>> data = null;

		if (existsCatch("welcome")) {
			String catchData = getCatch("welcome");
			data = toGson(catchData);
		} else {
			data = systemLogService.select();
			String catchData = toJson(data);
			putCatch("welcome", catchData);
		}
		view.put("data", data);
		view.put("count", data.size());
		view.put("ip", "");
		view.put("last_time", "");

		if (data != null) {
			view.put("ip", data.get(0).get("IP"));
			view.put("last_time", data.get(0).get("tim"));
		}
		addAllObjects(view);
		return getSuccess("index/welcome");
	}

}
